package cn.itcast.babasport.service.product;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.itcast.babasport.mapper.product.ProductMapper;
import cn.itcast.babasport.pojo.page.Pagination;
import cn.itcast.babasport.pojo.product.Product;
import cn.itcast.babasport.pojo.product.ProductQuery;
import cn.itcast.babasport.pojo.product.ProductQuery.Criteria;
@Service("productService")
public class ProductServiceImpl implements ProductService {

	@Resource
	private ProductMapper productMapper;

	@Override
	public Pagination selectProductListHavePage(String name, Long brandId, Boolean isShow, Integer pageNo) {
		ProductQuery productQuery = new ProductQuery();
		//封装查询条件
		Criteria criteria = productQuery.createCriteria();
		//构建分页的条件
		StringBuilder params = new StringBuilder();
		if (name != null && !"".equals(name)) {
			criteria.andNameLike("%"+name+"%");
			params.append("name=").append(name);
		}
		if (brandId != null ) {
			criteria.andBrandIdEqualTo(brandId);
			params.append("&brandId=").append(brandId);
		}
		if (isShow != null ) {
			criteria.andIsShowEqualTo(isShow);
			params.append("&isShow=").append(isShow);
		}else{
			criteria.andIsShowEqualTo(false);
			params.append("&isShow=").append(false);
		}
		//封装分页
		productQuery.setPageNo(Pagination.cpn(pageNo));
		productQuery.setPageSize(10);		
		//通过查询条件：结果集总条数	
		List<Product> list = productMapper.selectByExample(productQuery);
		//查询总记录数
		int totalCount = productMapper.countByExample(productQuery);
		//创建分页对象并填充数据
		Pagination pagination = new Pagination(productQuery.getPageNo(), productQuery.getPageSize(), totalCount,list);
		String url = "/product/list.do";
		pagination.pageView(url,params.toString());
		//返回分页对象
		return pagination;
	}

}
